package 多线程初阶.示例测试;

/**
 * wait 和 notify 的搭配使用
 */
public class TestDemo17 {
    static class WaitTask implements Runnable{
        Object locker=new Object();
        public WaitTask(Object locker){
            this.locker=locker;
        }
        @Override
        public void run() {
            synchronized (locker){
                while (true){
                    System.out.println(Thread.currentThread().getName()+"正在等待");
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"结束等待");
                }
            }
        }
    }
    static class NotifyTask implements Runnable{
        Object locker=new Object();
        public NotifyTask(Object locker){
            this.locker=locker;
        }
        @Override
        public void run() {
            synchronized (locker){
                System.out.println(Thread.currentThread().getName()+"开始notify");
                locker.notify();
                System.out.println(Thread.currentThread().getName()+"结束notify");
            }
        }
    }
    public static void main(String[] args) {
        Object locker=new Object();
        Thread waitThread=new Thread(new WaitTask(locker),"waitThread");
        Thread notifyThread=new Thread(new NotifyTask(locker),"notifyThread");
        waitThread.start();
        notifyThread.start();;
    }
}
